/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Plan.java
 *
 * Created on Sep 23, 2011, 2:48:25 PM
 */
package visuales;

import entidades.Bateria;
import entidades.Item;
import entidades.Jaula;
import entidades.TipoEvento;
import gestores.GestorProduccion;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author usuario
 */
public class Plan extends javax.swing.JFrame {

    private JFrame pantalla;
    private GestorProduccion gp;
    private DefaultTableModel dtm;

    /** Creates new form Plan */
    public Plan(JFrame padre) {
        initComponents();
        gp = new GestorProduccion();
        this.pantalla = padre;
        this.getPlan(false);

//        this.armarTabla();
    }

    public Plan() {
        initComponents();
        this.armarTabla();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        cbDestete = new javax.swing.JCheckBox();
        cbRalgro = new javax.swing.JCheckBox();
        cbPeleteo = new javax.swing.JCheckBox();
        btnVisualizar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Actividades Del Dia");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Actividades del Dia de Hoy"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Camara", "Bateria", "Jaula", "Actividad"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Actividad Por Evento"));

        cbDestete.setSelected(true);
        cbDestete.setText("Destete");

        cbRalgro.setSelected(true);
        cbRalgro.setText("Ralgro");

        cbPeleteo.setSelected(true);
        cbPeleteo.setText("Peleteo");

        btnVisualizar.setText("Visualizar");
        btnVisualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbDestete)
                .addGap(60, 60, 60)
                .addComponent(cbRalgro)
                .addGap(67, 67, 67)
                .addComponent(cbPeleteo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                .addComponent(btnVisualizar)
                .addGap(61, 61, 61))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbDestete)
                    .addComponent(cbRalgro)
                    .addComponent(cbPeleteo)
                    .addComponent(btnVisualizar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnImprimir.setText("Imprimir");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnImprimir)
                        .addGap(184, 184, 184)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnImprimir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarActionPerformed
        this.getPlan(true);
    }//GEN-LAST:event_btnVisualizarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
        pantalla.setVisible(true);
    }//GEN-LAST:event_btnSalirActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new Plan().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnVisualizar;
    private javax.swing.JCheckBox cbDestete;
    private javax.swing.JCheckBox cbPeleteo;
    private javax.swing.JCheckBox cbRalgro;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void armarTabla() {
        dtm = new DefaultTableModel();
        dtm.addColumn("Nombre");
        dtm.addColumn("Camara");
        dtm.addColumn("Bateria");
        dtm.addColumn("Jaula");
        dtm.addColumn("Actividad");
        jTable1.setModel(dtm);
    }

    private void mostrarDatos(boolean band) {
        ArrayList res_activades = null;
        if (band) {
            if (cbDestete.isSelected() != true && cbPeleteo.isSelected() != true && cbRalgro.isSelected() != true) {
                JOptionPane.showMessageDialog(null, "Seleccione un evento para ver los resultados.", "Atencion", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (cbDestete.isSelected() == true && cbPeleteo.isSelected() == true && cbRalgro.isSelected() == true) {
                    res_activades = this.gp.getPlanActividades(TipoEvento.DESTETE, TipoEvento.PELETEO, TipoEvento.RALGRO);
                }
                if (cbDestete.isSelected() == true && cbPeleteo.isSelected() != true && cbRalgro.isSelected() != true) {
                    res_activades = this.gp.getPlanActividades(TipoEvento.DESTETE, -1, -1);
                }
                if (cbDestete.isSelected() == true && cbPeleteo.isSelected() != true && cbRalgro.isSelected() == true) {
                    res_activades = this.gp.getPlanActividades(TipoEvento.DESTETE, -1, TipoEvento.RALGRO);
                }
                if (cbDestete.isSelected() == true && cbPeleteo.isSelected() == true && cbRalgro.isSelected() != true) {
                    res_activades = this.gp.getPlanActividades(TipoEvento.DESTETE, TipoEvento.PELETEO, -1);
                }
                if (cbDestete.isSelected() != true && cbPeleteo.isSelected() != true && cbRalgro.isSelected() == true) {
                    res_activades = this.gp.getPlanActividades(-1, -1, TipoEvento.RALGRO);
                }
                if (cbDestete.isSelected() != true && cbPeleteo.isSelected() == true && cbRalgro.isSelected() != true) {
                    res_activades = this.gp.getPlanActividades(-1, TipoEvento.PELETEO, -1);
                }
                if (cbDestete.isSelected() != true && cbPeleteo.isSelected() == true && cbRalgro.isSelected() == true) {
                    res_activades = this.gp.getPlanActividades(-1, TipoEvento.PELETEO, TipoEvento.RALGRO);
                }
                if (res_activades != null && res_activades.isEmpty() == false) {
                    Iterator iter_resActividades = res_activades.iterator();
                    Item res_item = null;

                    Object fila[] = new Object[5];

                    while (iter_resActividades.hasNext()) {
                        res_item = (Item) iter_resActividades.next();
                        fila[0] = res_item.getAnimal().getNombre();

                        Bateria res_bateria = (Bateria) res_item.getAnimal().getUbicacion().getBateria().get(0);
                        Jaula res_jaula = res_bateria.getJaula().get(0);

                        fila[1] = res_item.getAnimal().getUbicacion().getNombre();
                        fila[2] = res_bateria.getNombre();
                        fila[3] = res_jaula.getNombre();
                        fila[4] = res_item.getTipo_evento();

                        dtm.addRow(fila);

                        jTable1.setModel(dtm);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "No hay actividades para el dia de la fecha", "Atencion", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    }

    private void getPlan(boolean band) {
        this.armarTabla();
        this.mostrarDatos(band);
    }
}
